package ldh.im.fxbase.util;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDialog;
import com.jfoenix.controls.JFXDialogLayout;
import io.github.palexdev.materialfx.controls.MFXButton;
import io.github.palexdev.materialfx.dialogs.*;
import io.github.palexdev.materialfx.enums.ScrimPriority;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Modality;
import org.kordamp.ikonli.javafx.FontIcon;

import java.util.Map;
import java.util.function.Consumer;

public class DialogUtil {

    public static void alert(String header, String content) {
        alert(StageUtil.getRoot(), header, content);
    }

    public static void alert(StackPane root, String header, String content) {
        JFXDialog dialog = new JFXDialog();
//        dialog.setOverlayClose(false);
        JFXDialogLayout body = new JFXDialogLayout();
        body.setHeading(new Label(header));
        body.setBody(new Label(content));
        dialog.setContent(body);
        dialog.show(root);
    }

    public static void show(StackPane root, String header, Node content) {
        JFXDialog dialog = new JFXDialog();
//        dialog.setOverlayClose(false);
        JFXDialogLayout body = new JFXDialogLayout();
        body.setHeading(new Label(header));
        body.setBody(content);
        dialog.setContent(body);
        dialog.show(root);
    }

    public static void alert(String header, String content, boolean model, Map<String, Consumer<JFXDialog>> consumerMap) {
        show(header, new Label(content), model, consumerMap);
    }

    public static JFXDialog show(String header, Node content, boolean model, Map<String, Consumer<JFXDialog>> consumerMap) {
        JFXDialog dialog = new JFXDialog();
        dialog.setOverlayClose(model);
        JFXDialogLayout body = new JFXDialogLayout();
        if (header != null) {
            body.setHeading(new Label(header));
        }
        if (consumerMap != null) {
            for(Map.Entry<String, Consumer<JFXDialog>> entry : consumerMap.entrySet()) {
                JFXButton jfxButton = new JFXButton(entry.getKey());
                jfxButton.setOnAction(e->{
                    dialog.close();
                    entry.getValue().accept(dialog);
                });
                body.getActions().add(jfxButton);
            }
        }

        body.setBody(content);
        body.setPadding(new Insets(0, 10, 10, 0));
        dialog.setContent(body);
        dialog.show(StageUtil.getRoot());
        return dialog;
    }

    public static JFXDialog show(Region content, boolean model) {
        JFXDialog dialog = new JFXDialog();
        dialog.setOverlayClose(model);
        dialog.setContent(content);
        dialog.show(StageUtil.getRoot());
        return dialog;
    }

    public static JFXDialog show(StackPane root, Region content, boolean model) {
        JFXDialog dialog = new JFXDialog();
        dialog.setOverlayClose(model);
        dialog.setContent(content);
        dialog.show(root);
        return dialog;
    }

    public static void showSuccess(StackPane dialogRoot, String message, double maxWidth) {
        StackPane root = new StackPane();
        Label label = new Label();
        label.setText(message);
        FontIcon fontIcon = IconUtil.createIcon("ion-checkmark-round", 36, Color.BLACK);
        label.setGraphic(fontIcon);
        label.setContentDisplay(ContentDisplay.TOP);
        root.getChildren().setAll(label);

        JFXDialog dialog = new JFXDialog();
        dialog.setMaxWidth(maxWidth);
        dialog.getStyleClass().add("jfx-dialog-success");
//        dialog.setOverlayClose(true);
        JFXDialogLayout body = new JFXDialogLayout();
        body.setBody(root);
        dialog.setContent(body);
        dialog.show(dialogRoot);
        ThreadPoolUtil.schedule(()-> FxThreadUtil.runLater(()->dialog.close()), 1500);
    }

    public static void showSuccess(StackPane dialogRoot, String message) {
        showSuccess(dialogRoot, message, 200);
    }

    public static void showError(StackPane dialogRoot, String message, double maxWidth) {
        StackPane root = new StackPane();
        Label label = new Label();
        label.setText(message);
        FontIcon fontIcon = IconUtil.createIcon("ion-close-round", 36, Color.BLACK);
        label.setGraphic(fontIcon);
        label.setContentDisplay(ContentDisplay.TOP);
        root.getChildren().setAll(label);

        JFXDialog dialog = new JFXDialog();
        dialog.getStyleClass().add("jfx-dialog-error");
        dialog.setOverlayClose(false);
        JFXDialogLayout body = new JFXDialogLayout();
        body.setBody(root);
        dialog.setContent(body);
        dialog.show(dialogRoot);
        ThreadPoolUtil.schedule(()-> FxThreadUtil.runLater(()->dialog.close()), 3000);
    }

    public static void showError(StackPane dialogRoot, String message) {
       showError(dialogRoot, message, 200);
    }

    public static void dialog(Pane node, String title, String content) {
        dialog(node, title, content, null);
    }

    public static void dialog(Pane node, String title, String content, Consumer<MFXGenericDialog> confirmAction) {
        MFXGenericDialog dialogContent = MFXGenericDialogBuilder.build()
                .setContentText(content)
                .makeScrollable(true)
                .get();
        MFXStageDialog dialog = MFXGenericDialogBuilder.build(dialogContent)
                .toStageDialogBuilder()
                .initOwner(StageUtil.getStage())
                .initModality(Modality.APPLICATION_MODAL)
                .setDraggable(true)
                .setTitle(title)
                .setOwnerNode(node)
                .setScrimPriority(ScrimPriority.WINDOW)
                .setScrimOwner(true)
                .get();

        MFXButton confirmButton = new MFXButton("Confirm");
        confirmButton.getStyleClass().add("outline-button");
        MFXButton cancelButton = new MFXButton("Cancel");
        cancelButton.getStyleClass().add("outline-button");
        dialogContent.addActions(
                Map.entry(confirmButton, event -> {
                    if (confirmAction != null) {
                        confirmAction.accept(dialogContent);
                    }
                }),
                Map.entry(cancelButton, event -> dialog.close())
        );

        dialog.showDialog();
    }

    public static void dialog(Pane node, String title, Node content) {
        dialog(node, title, content, null);
    }

    public static void dialog(Pane node, String title, Node content, Consumer<MFXGenericDialog> initDialog) {
        MFXGenericDialog dialogContent = MFXGenericDialogBuilder.build()
                .setContent(content)
                .makeScrollable(true)
                .get();
        MFXStageDialog dialog = MFXGenericDialogBuilder.build(dialogContent)
                .toStageDialogBuilder()
                .initOwner(StageUtil.getStage())
                .initModality(Modality.APPLICATION_MODAL)
                .setDraggable(true)
                .setTitle(title)
                .setOwnerNode(node)
                .setScrimPriority(ScrimPriority.WINDOW)
                .setScrimOwner(true)
                .get();

        if (initDialog != null) {
            initDialog.accept(dialogContent);
        }

        dialog.showDialog();
    }

    public static void stageAlert(String title, String content) {
        Dialog dialog = new Dialog();
        dialog.setTitle(title);
        dialog.setContentText(content);
        ButtonType okType = new ButtonType("确定", ButtonBar.ButtonData.OK_DONE);
        ButtonType closeType = new ButtonType("关闭", ButtonBar.ButtonData.CANCEL_CLOSE);
        dialog.getDialogPane().getButtonTypes().addAll(closeType, okType);
        dialog.show();
    }

}
